﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using XemlInteractiveDesigner;
using Mpi.Common;
using Xeml.Manager;
using CompilerControls;

namespace XemlCompiler {
    public partial class MainForm : KryptonForm {
        private FileManager _fMgr = new FileManager();

        public MainForm() {
            InitializeComponent();
            // int x = SpManager.Instance.ProviderComponents.Count; // what is this doing here??
            _fMgr.FileLoaded += new EventHandler(OnFileLoaded);
            _fMgr.FilePathNeeded += new EventHandler<FilePathNeededEventArgs>(_fMgr_FilePathNeeded);
            _fMgr.FileSaved +=new EventHandler(_fMgr_FileSaved);
            _fMgr.SomethingChanged +=new EventHandler(_fMgr_SomethingChanged);
        }

        void _fMgr_FilePathNeeded(object sender, FilePathNeededEventArgs e)
        {
            saveFileDialogDocument.FileName = e.Path;
            if (saveFileDialogDocument.ShowDialog() == DialogResult.OK)
            {
                e.Path = saveFileDialogDocument.FileName;
            }
            else
                e.Cancel = true;
        }

        void _fMgr_FileSaved(object sender, EventArgs e)
        {
            this.Text = this.Text.Replace(" (unsaved)", "");
        }

        void _fMgr_SomethingChanged(object sender, EventArgs e)
        {
            this.Text = this.Text + " (unsaved)";
        }

        void OnFileLoaded(object sender, EventArgs e)
        {
            providerManager1.Setup(_fMgr.CurrentXemlDoc);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CheckUnsavedChanges())
            {
                if (openFileDialogDoc.ShowDialog() == DialogResult.OK)
                {
                    _fMgr.LoadDocument(openFileDialogDoc.FileName, false);
                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _fMgr.SaveDocument();
        }

        private void providerManager1_SelectedProviderChanged(object sender, EventArgs e)
        {
            dataMappingCtrl1.Setup(_fMgr.CurrentXemlDoc, providerManager1.SelectedProvider);
        }

        private void dataMappingCtrl1_Load(object sender, EventArgs e)
        {

        }

        private void kryptonNavigator1_SelectedPageChanged(object sender, EventArgs e)
        {
            if (kryptonNavigator1.SelectedPage == kryptonPage2)
                compileCtrl1.Setup(_fMgr.CurrentXemlDoc);
        }

        private bool CheckUnsavedChanges()
        {
            if (_fMgr.UnsavedChanges)
            {
                DialogResult dr = MessageBox.Show("You have unsaved changes in the current document. Do you want save now?", "Save needed", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                    return false;
                if (dr == DialogResult.Yes)
                    _fMgr.SaveDocument();
            }
            return true;
        }

        /// <summary>
        /// The application is closing
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = !CheckUnsavedChanges();
            base.OnClosing(e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CheckUnsavedChanges())
            {
                providerManager1.Setup(null);
                dataMappingCtrl1.Setup(null, null);
            }
        }

        private void showPoviderMgrLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataProviderMgrLog dpml = new DataProviderMgrLog();
            dpml.ShowDialog();
        }
    }
}
